<template>
  <div class="about">
    <div class="box">
      <div class="warp">
        <div class="logo">
          <img src="../assets/jiazhenglogo.png" alt="" />
        </div>
        <div class="img">
          <img src="../assets/jiazheng.jpg" alt="" />
        </div>
        <div class="denglu">
          <h2>账号登录</h2>
          <div class="input">
            <input
              type="text"
              placeholder="账号"
              v-model="uid"
              @blur="uidkong"
            />
            <div class="uidk" v-show="uidk">账号不能为空</div>
          </div>
          <div class="input">
            <input
              type="password"
              placeholder="密码"
              v-model="password"
              @blur="passwordkong"
            />
            <div class="uidk" v-show="passwordk">密码不能为空</div>
            <div class="uidk" v-show="passwordc">
              密码必须同时包含数字，字母
            </div>
          </div>
          <div class="input">
            <input
              class="yzm"
              type="text"
              placeholder="验证码"
              v-model="yzm"
              @blur="yzmkong"
            />
            <div id="yan"></div>
            <div class="uidk" v-show="yzmk">验证码不能为空</div>
          </div>
          <div class="input">
            <!-- <span class="wang a" @click="wangji">忘记密码？</span> -->
            <span class="zhuce a" @click="zhuce">立即注册</span>
            <button @click="signIn">登录</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapMutations } from "vuex";
import { GVerify } from "./verifyCode";
export default {
  data() {
    return {
      gverify: {},
      uid: "",
      password: "",
      yzm: "",
      uidk: false,
      passwordk: false,
      passwordc: false,
      yzmk: false,
    };
  },
  methods: {
    ...mapMutations(["typeWhat"]),
    uidkong() {
      if (this.uid == "") {
        this.uidk = true;
      } else {
        this.uidk = false;
      }
    },
    passwordkong() {
      if (this.password == "") {
        this.passwordc = false;
        this.passwordk = true;
      } else {
        this.passwordk = false;
        var pattern = /^[\w_-]{6,16}$/;
        if (!pattern.test(this.password)) {
          this.passwordc = true;
        } else {
          this.passwordc = false;
        }
      }
    },
    yzmkong() {
      if (this.yzm == "") {
        this.yzmk = true;
      } else {
        this.yzmk = false;
      }
    },
    zhuce() {
      this.$router.push("/zhuce");
    },
    wangji() {
      this.$router.push("/forget");
    },
    signIn() {
      console.log(this.gverify.options.code);
      var a = this.gverify.options.code;
      a = a.substr(a.length - 4, a.length);
      if (this.yzm == a) {
        this.myaxios({
          url: "http://housekeepi.com/api/login",
          method: "post",
          data: {
            name: this.uid,
            pw: this.password,
          },
        })
          .then(
            (res, config) => {
              if (res.data.data.token == undefined) {
                this.$message.error(res.data.msg);
              } else {
                console.log(res.data.data.token);
                window.localStorage.setItem("token", res.data.data.token);
                window.localStorage.setItem("type", res.data.data.user_type);
                window.localStorage.setItem(
                  "worktype",
                  res.data.data.work_type
                );
                window.localStorage.setItem(
                  "pusername",
                  res.data.data.nick_name
                );
                window.localStorage.setItem("password", res.data.data.pw);
                window.localStorage.setItem("icon", res.data.data.icon);
                window.localStorage.setItem(
                  "personmsg",
                  JSON.stringify({
                    phone: res.data.data.phone,
                    nick_name: res.data.data.nick_name,
                    h_price: res.data.data.h_price,
                    user_id: res.data.data.user_id,
                    work_type: res.data.data.work_type,
                    age:res.data.data.age,
                    home:
                      res.data.data.province + "/" +
                      res.data.data.city + "/" +
                      res.data.data.area,
                    ctime: res.data.data.create_time,
                    ltime: res.data.data.last_login_time,
                  })
                );
                // this.typeWhat(res.data.data.user_type);
                // console.log(this.$store.state.type);
                if (res.data.data.user_type == 1) {
                  this.$router.push("/order1");
                } else {
                  this.$router.push("/order");
                }
              }
            },
            () => {
              console.log(1);
            }
          )
          .catch(() => {
            console.log(2);
          });
      } else {
        this.$message.error("验证码错误！");
        var yanDiv = document.querySelector("#yan");
        yanDiv.innerHTML = "";
        this.gverify = new GVerify({ id: "yan" });
      }
    },
  },
  mounted() {
    this.gverify = new GVerify({ id: "yan" });
  },
};
</script>
<style scoped>
* {
  margin: 0;
  padding: 0;
  text-align: center;
}
.a:hover {
  color: skyblue;
  cursor: pointer;
}
.wang {
  display: block;
  position: absolute;
  top: 0px;
  right: 4px;
  font-size: 14px;
  color: rgb(102, 102, 102);
}
.zhuce {
  display: block;
  position: absolute;
  top: 0px;
  left: 8px;
  font-size: 14px;
  color: rgb(102, 102, 102);
}
.uidk {
  font-size: 14px;
  color: red;
  position: absolute;
  top: 47px;
  left: 4px;
}
.box {
  width: 100%;
  height: 600px;
  background-color: rgb(245, 111, 124);
}
.warp {
  position: relative;
  width: 1200px;
  height: 600px;
  margin: 100px auto 0px;
  background-color: rgb(245, 111, 124);
}
.logo {
  position: absolute;
  top: -100px;
}
.denglu {
  position: absolute;
  top: 40px;
  right: 100px;
  box-sizing: border-box;
  padding: 30px 25px 30px 25px;
  width: 360px;
  height: 500px;
  background-color: #fff;
  border-radius: 5px;
}
.input {
  position: relative;
  margin-top: 30px;
}
.img {
  position: absolute;
  top: 80px;
  left: 30px;
}
input {
  font-size: 18px;
  text-indent: 10px;
  text-align: left;
  width: 300px;
  height: 40px;
  border: 1px solid rgb(230, 230, 230);
}
#yan {
  position: absolute;
  top: -5px;
  right: 5px;
}
.yzm {
  display: block;
  width: 120px;
  margin-left: 4px;
}
button {
  margin-top: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 22px;
  width: 300px;
  height: 40px;
  border: none;
  background-color: rgb(245, 84, 106);
  border-radius: 5px;
  color: #fff;
}
</style>
